//
//  U9ImageAlertView.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/3/21.
//  Copyright © 2017年 Jason. All rights reserved.
//

#import "U9ImageAlertView.h"

static U9ImageAlertView *_shardAlert = nil;

@interface U9ImageAlertView()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *imageView;

@end


@implementation U9ImageAlertView

+ (instancetype)sharedImageAlertView{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_shardAlert) {
            _shardAlert = [[self alloc] init];
        }
        
    });
    return _shardAlert;
}

- (instancetype)init{
    if (self = [super init]) {
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [delegate.window addSubview:self];
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
        [self addSubViews];
        
        UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissAlert)];
        [self addGestureRecognizer:tapper];
        self.hidden = YES;
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

- (void)setType:(AlertImageType)type{
    _type = type;
    switch (type) {
        case AlertSuccess:
            self.imageView.image = [UIImage imageNamed:@"alert_success"];
            break;
        case AlertFail:
            self.imageView.image = [UIImage imageNamed:@"alert_fail"];
            break;
        case AlertGuess:
            self.imageView.image = [UIImage imageNamed:@"alert_guess"];
            break;
        case AlertLogin:
            self.imageView.image = [UIImage imageNamed:@"alert_login"];
            break;
        default:
            break;
    }
}

- (void)addSubViews{
    self.imageView= [[UIImageView alloc] init];
    [self addSubview:self.imageView];
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = UIColorFromRGB(kAllBlackColor);
    [self.imageView addSubview:self.titleLabel];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(300, 233));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.imageView.mas_centerX);
        make.top.equalTo(self.imageView).mas_equalTo(160);
    }];
}


- (void)showAlert:(AlertBlock)block{
    [UIView animateWithDuration:0.8 animations:^{
        self.hidden = NO;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissAlert:block];
        });
    }];
}


- (void)dismissAlert:(AlertBlock)block{
    [UIView animateWithDuration:0.8 animations:^{
        block();
        [self dismissAlert];
    }];
}

- (void)dismissAlert{
    self.hidden = YES;
}


@end
